package com.qymerge.manage.core.utils;

import com.qymerge.manage.core.exception.ApiException;

import java.util.List;
import java.util.Map;

/**
 * Desc 请求入参验证
 */
public class RequestCheckUtils {

    public static void checkNotEmptyWithParamFormat(Object... values) throws ApiException {
        for (Object value : values) {
            checkNotEmpty(value, "参数格式不合法");
        }
    }

    public static void checkNotEmpty(Object value) throws ApiException {
        checkNotEmpty(value, "参数异常");
    }

    public static void checkNotEmpty(Object value, String errorMsg) throws ApiException {
        if (value == null) {
            throw new ApiException(0, errorMsg);
        } else if (((value instanceof String)) && (((String) value).trim().length() == 0)) {
            throw new ApiException(0, errorMsg);
        } else if ((value instanceof Long) && (((Long) value).doubleValue() <= 0)) {
            throw new ApiException(0, errorMsg);
        } else if ((value instanceof Float) && (((Float) value) <= 0)) {
            throw new ApiException(0, errorMsg);
        } else if ((value instanceof Integer) && (((Integer) value) <= 0)) {
            throw new ApiException(0, errorMsg);
        } else if ((value instanceof List) && (((List) value).isEmpty())) {
            throw new ApiException(0, errorMsg);
        } else if ((value instanceof Map) && (((Map) value).isEmpty())) {
            throw new ApiException(0, errorMsg);
        }
    }

    public static void checkNotFalse(boolean value, String errorMsg) throws ApiException {
        if (!value) {
            throw new ApiException(0, errorMsg);
        }
    }

}
